package cn.zhizihua.service.impl;

import cn.zhizihua.exception.AppException;
import cn.zhizihua.exception.AppExceptionCodeMsg;
import cn.zhizihua.mapper.UserMapper;
import cn.zhizihua.pojo.User;
import cn.zhizihua.query.BaseQuery;
import cn.zhizihua.query.UserQuery;
import cn.zhizihua.service.IUserService;
import cn.zhizihua.vo.PageBean;
import cn.zhizihua.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService extends BaseService<User> implements IUserService {

    @Autowired
    UserMapper userMapper;


    //登录校验
    @Override
    public ResultBean login(String userName, String password) {
        //参数校验
        if (userName==null || "".equals(userName) ||
                password==null || "".equals(password)){
            //抛自定义异常 参数为空
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        //通过用户名查询用户是否存在
        User user = userMapper.getByUserName(userName);

        // 用户校验
        if (user==null){
            // 抛自定义异常 用户名不存在
            throw new AppException(AppExceptionCodeMsg.USERNAME_NOT_EXISTS);
        }

        if(!user.getPassword().equals(password)){
            // 抛自定义异常 密码错误
            throw new AppException(AppExceptionCodeMsg.PASSWORD_ERROR);
        }

        //密码不给到前端  前端获取需要加session
        user.setPassword(null);

        return ResultBean.SUCCESS(user);
    }
}
